package com.cloud.api.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.cloud.api.config.KeyConfiguration;
import com.cloud.common.exception.CloudException;
import com.cloud.common.util.jwt.IJWTInfo;
import com.cloud.common.util.jwt.JWTHelper;
import com.cloud.common.util.jwt.RsaKeyHelper;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class JwtTokenUtil {
	@Value("${jwt.expire}")
	private int expire;
	@Autowired
	private KeyConfiguration keyConfiguration;
//
//	@Autowired
//	private RedisTemplate<String, Object> redisTemplate;

	public String generateToken(IJWTInfo jwtInfo) throws Exception {
		return JWTHelper.generateToken(jwtInfo, keyConfiguration.getUserPriKey(), expire);
	}

	public IJWTInfo getInfoFromToken(String token) throws CloudException {
		try {
			return JWTHelper.getInfoFromToken(token, keyConfiguration.getUserPubKey());
		}catch(Exception e) {
			log.error("解析token 错误",e);
			throw new CloudException(e.getMessage());
		}
	}
}
